<template>
  <div>
    ods_ids: {{ ods_ids }}
    <a-button @click="showOds2so"> showOds2so </a-button>

    <div>merge_type: {{ values.merge_type }}</div>
    <div>merge_check: {{ values.merge_check }}</div>

    <div>公司: {{ values.company_id }}</div>

    <a-form :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-model-item label="发票类型">
        <!-- {{ values.invoice_type }} -->
        <!-- @change="handleChange" -->
        <a-select v-model="invoice_type_value" @change="handleChange">
          <template v-for="op in invoice_type_selection">
            <a-select-option :key="op[0]" :value="op[0]">
              {{ op[1] }}
            </a-select-option>
          </template>

          <!-- <template v-for="op in fieldInfo.selection || []">
            <a-select-option :key="op[0]" :value="op[0]">
              {{ op[1] }}
            </a-select-option>
          </template> -->
        </a-select>
      </a-form-model-item>
    </a-form>

    <div>销售明细行 {{ values.line_ids }}</div>

    <a-button @click="ods2so"> ods2so </a-button>

    <!-- <a-button @click="selectSome"> selectSome </a-button> -->
  </div>
</template>

<script>
import api from '@/odoorpc'

export default {
  name: 'NcTreeView',
  components: {},
  mixins: [],
  props: {
    ods_ids: { type: Array, default: () => [] }
  },

  data() {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },

      view: undefined,
      values: {},
      invoice_type_value: ''

      // so_records: []
    }
  },

  computed: {
    invoice_type_selection() {
      return [
        ['vat_special', '增值税专用发票'],
        ['vat_general', '增值税普通发票'],
        ['vat_general_e', '增值税电子普通发票'],
        ['vat_special_e', '增值税电子专用发票']
      ]
    }
  },

  created() {},

  mounted() {
    // console.log('mounted', this.$route.fullPath)
  },

  methods: {
    async showOds2so() {
      const view = api.env.wizardview('fp_sale.action_ods_to_sale_order', {
        active_ids: this.ods_ids
      })

      const fields = await view.load_fields()
      // console.log(fields)

      const line_ids_field_info = fields.line_ids
      const ods_line_ids_field_info = fields.ods_line_ids

      const line_ids_relation = api.env.relation(line_ids_field_info, {
        parent: view.view_info
      })

      await line_ids_relation.load_views()

      const ods_line_ids_relation = api.env.relation(ods_line_ids_field_info, {
        parent: view.view_info
      })

      await ods_line_ids_relation.load_views()

      const relations = {
        line_ids: line_ids_relation,
        ods_line_ids: ods_line_ids_relation
      }

      view.load_relations_done(relations)
      // console.log(view)

      const res = await view.onchange_new()
      // console.log(res)

      const { values } = res
      const line_ids_value = values.line_ids
      delete values.line_ids

      // const line_ids = res.values.line_ids
      const { values: line_ids_records } =
        line_ids_relation.tree.read_for_new_o2m(line_ids_value)
      // console.log(line_ids_records)

      const line_ids_display_values = line_ids_relation.tree.values_display(
        line_ids_records,
        []
      )

      // console.log(line_ids_display_values)

      const test_data = [line_ids_display_values[0]]

      this.view = view
      this.values = { ...values, line_ids: test_data }

      this.invoice_type_value = values.invoice_type
    },

    async handleChange(value, ...args) {
      console.log(value, ...args)
      console.log(this.invoice_type_value)
      await this.view.onchange('invoice_type', {
        value: this.invoice_type_value
      })
      // const res = await this.view.commit()
      // console.log(res)
    },

    async ods2so() {
      const res2 = await this.view.wizard_button_clicked({
        name: 'create_sale_order',
        type: 'object'
      })

      const so_ids = await this.get_so_ids()

      this.$emit('button-click', so_ids)
    },

    async get_so_ids() {
      const ods_ids = this.ods_ids
      const Model = api.env.model('fp.ods.sale.order')
      const ods_records = await Model.read(ods_ids, {
        fields: ['sale_order_id']
      })

      const sale_order_ids2 = ods_records.map(item => item.sale_order_id)
      const sale_order_ids = sale_order_ids2.map(item => item[0])
      // console.log(sale_order_ids)

      return sale_order_ids
    }
  }
}
</script>

<style type="text/css"></style>
